﻿using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.Text;
using Bintje.bintje;


namespace Bintje
{
    public sealed class sessionSerializer
    {
        private static String UTF8BytesToString(Byte[] bytes)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            String resultString = encoding.GetString(bytes);
            return resultString;
        }

        private static Byte[] StringToUTF8Bytes(String xmlString)
        {
            UTF8Encoding encoding = new UTF8Encoding();
            Byte[] resultBytes = encoding.GetBytes(xmlString);
            return resultBytes;
        }

        public static string SerializeSession(imageoSession session)
        {
            MemoryStream memStream = new MemoryStream();
            XmlSerializer serializer = new XmlSerializer(typeof(imageoSession));
            XmlTextWriter textWriter = new XmlTextWriter(memStream, Encoding.UTF8);
            serializer.Serialize(textWriter, session);
            memStream = (MemoryStream)textWriter.BaseStream;
            return UTF8BytesToString(memStream.ToArray());
        }

        public static imageoSession DeserializeSession(string xml)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(imageoSession));
            MemoryStream memStream = new MemoryStream(StringToUTF8Bytes(xml));
            XmlTextWriter textWriter = new XmlTextWriter(memStream, Encoding.UTF8);
            return (imageoSession)serializer.Deserialize(memStream);
        }

    }
}
